﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;
using System.Drawing;

namespace DataLib.Accounts
{
    public partial class PermissionAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetStyle();
                //Control c = (Control)Page.Master.FindControl("Content2");
                //ImageButton btn = (ImageButton)c.FindControl("BtnAddCategory");
                //btn.Attributes.Add("onclick", "return confirm('你是否确定删除这条记录？');");
                CategoriesDatabind();
                if (this.ClassList.SelectedItem != null)
                {
                    PermissionsDatabind();
                }
            }

        }

        #region 初始化
        private void SetStyle()
        {
            DataGrid1.BorderWidth = Unit.Pixel(1);
            DataGrid1.CellPadding = 4;
            DataGrid1.CellSpacing = 0;
            //DataGrid1.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
            //DataGrid1.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());

        }

        private void CategoriesDatabind()
        {
            DataSet CategoriesList = AccountsTool.GetAllCategories();
            this.ClassList.DataSource = CategoriesList;
            this.ClassList.DataTextField = "Description";
            this.ClassList.DataValueField = "CategoryID";
            this.ClassList.DataBind();
        }

        private void PermissionsDatabind()
        {
            int CategoryId = int.Parse(this.ClassList.SelectedValue);
            DataSet PermissionsList = AccountsTool.GetPermissionsByCategory(CategoryId);
            this.DataGrid1.DataSource = PermissionsList;
            this.DataGrid1.DataBind();
        }

        #endregion



        protected void ClassList_SelectedIndexChanged(object sender, EventArgs e)
        {
            PermissionsDatabind();
        }

        protected void BtnAddCategory_Click(object sender, EventArgs e)
        {
            string Category = this.CategoriesName.Text.Trim();
            if (Category != "")
            {
                PermissionCategories c = new PermissionCategories();
                c.Create(Category);
                CategoriesDatabind();
                if (this.ClassList.SelectedItem != null)
                {
                    PermissionsDatabind();
                }
                this.CategoriesName.Text = "";
            }
            else
            {
                this.lbltip1.Text = "名称不能为空！";
            }
        }

        protected void BtnAddPermissions_Click(object sender, EventArgs e)
        {
            string Permissions = this.PermissionsName.Text.Trim();
            if (Permissions != "")
            {
                int CategoryId = int.Parse(this.ClassList.SelectedValue);
                Permissions p = new Permissions();
                p.Create(CategoryId, Permissions);
                if (this.ClassList.SelectedItem != null)
                {
                    PermissionsDatabind();
                }
                this.PermissionsName.Text = "";
            }
            else
            {
                this.lbltip2.Text = "名称不能为空！";
            }

        }

        protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.EditItem:
                    ImageButton btn = (ImageButton)e.Item.FindControl("btnDelete");
                    btn.Attributes.Add("onclick", "return confirm('你是否确定删除这条记录？');");
                    break;

            }

        }

        protected void BtnDelCategory_Click(object sender, ImageClickEventArgs e)
        {
            int CategoryId = int.Parse(this.ClassList.SelectedValue);
            PermissionCategories c = new PermissionCategories();
            c.Delete(CategoryId);
            CategoriesDatabind();
            if (this.ClassList.SelectedItem != null)
            {
                PermissionsDatabind();
            }

        }

        protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string c = e.CommandName;
            int PermissionsID = (int)this.DataGrid1.DataKeys[e.Item.ItemIndex];
            string Permissions = e.Item.Cells[1].Text.Trim();
            switch (c)
            {
                case "Delete":
                    if (Permissions != "帐户管理")
                    {
                        Permissions p = new Permissions();
                        p.Delete(PermissionsID);
                        PermissionsDatabind();
                    }
                    else
                    {
                        Response.Write("<script>alert('对不起,不能删除这个权限,否则将无法进行权限管理!');</script>");
                    }
                    break;
                case "Edit":
                    //this.TabEdit.Visible=true;
                    //this.lblPermId.Text=PermissionsID.ToString();
                    //this.txtNewName.Text=Permissions;
                    break;
            }


        }

        protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            string id = e.Item.Cells[0].Text;
            TextBox bTextBox = (TextBox)(e.Item.Cells[1].Controls[0]);
            string Permissions = bTextBox.Text.Trim();
            if ((Permissions != "") && (id != ""))
            {
                Permissions p = new Permissions();
                p.Update(int.Parse(id), Permissions,"");
            }
            //恢复状态
            DataGrid1.EditItemIndex = -1;
            PermissionsDatabind();
        }

        protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
        {
            DataGrid1.EditItemIndex = e.Item.ItemIndex;
            PermissionsDatabind();

        }

        protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            DataGrid1.EditItemIndex = -1;
            PermissionsDatabind();

        }
    }
}